import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Banner, HotTag, SongSheet } from './data-types/common.types';
import { API_CONFIG, ServiceModule } from './service.module';
import { map } from 'rxjs/internal/operators'

@Injectable({
  providedIn: ServiceModule,//是哪个模块提供的
})
export class HomeService {

  constructor(private http: HttpClient, @Inject(API_CONFIG) private url: String) { }

  getBanners(): Observable<Banner[]> {
    // 要返回banner数组 要指明数据类型
    return this.http.get(this.url + '/banner')
      .pipe(map((res: { banners: Banner[] }) => res.banners))
  }

  // 获取热门标签
  getHotTags(): Observable<HotTag[]> {
    return this.http.get(this.url + '/playlist/hot')
      .pipe(map((res: { tags: HotTag[] }) => {
        return res.tags.sort((x: HotTag, y: HotTag) => {
          return x.position - y.position
        }).slice(0, 5);
      }))
  }

  // 获取热门歌单
  getPersonalSheetList(): Observable<SongSheet[]> {
    return this.http.get(this.url + '/personalized')
      .pipe(map((res: { result: SongSheet[] }) => res.result.slice(0, 16)))
  }
}
